package com.qdlc.p2p.dal.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.util.NumberUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.AdditionalFlowProjectDao;
import com.qdlc.p2p.dal.dto.AdditionalFlowProject;
import com.qdlc.p2p.dal.model.FlowProjectModel;

/**
 * 理财项目DAO接口
 * @author wjx
 * @version 2.0
 * @since 2015年05月15日 11时26分30秒
 */
@Repository(value = "additionalFlowProjectDao")
public class AdditionalFlowProjectDaoImpl extends BaseDaoImpl<AdditionalFlowProject> implements AdditionalFlowProjectDao {

    @SuppressWarnings("unchecked")
    @Override
    public AdditionalFlowProject getAdditionalFlowProjectById(long id) {
        String jpql = "from AdditionalFlowProject where id = ?1";
        Query query = em.createQuery(jpql);
        query.setParameter(1, id);
        List<AdditionalFlowProject> list = query.getResultList();
        if (list != null && list.size() >= 0) {
            return (AdditionalFlowProject) list.get(0);
        } else {
            return null;
        }
    }

	@SuppressWarnings("unchecked")
	@Override
	public AdditionalFlowProject getFlowProjectByUuid(String uuid) {
		String jpql = "from AdditionalFlowProject where uuid = ?1";
		Query query = em.createQuery(jpql);
		query.setParameter(1, uuid);
		 List<AdditionalFlowProject> list = query.getResultList();
	        if (list != null && list.size() > 0) {
	            return (AdditionalFlowProject) list.get(0);
	        } else {
	            return null;
	        }
	}

	@Override
	public int updateFlowProjectTotal(long pid, int total) {
		String jpql = "update AdditionalFlowProject set yesCopies = yesCopies+ ?1 where id = ?2";
		Query query = em.createQuery(jpql);
		query.setParameter(1, total);
		query.setParameter(2, pid);
		int result = query.executeUpdate();
		return result;
	}

	@Override
	public int updateFlowProjectStatus(byte status, long id) {
		String jpql = "update AdditionalFlowProject set status =?1 where id =?2";
		Query query = em.createQuery(jpql);
		query.setParameter(1, status);
		query.setParameter(2, id);
		int result = query.executeUpdate();
		return result;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<FlowProjectModel> getIndexFlow() {
		String jpql = "select * from " + Global.DB_PREFIX + "additional_flow_project where status in(1,3,6) ORDER BY status ASC ,add_time ASC LIMIT 0,4";
		Query query = em.createNativeQuery(jpql);
		List  list =query.getResultList();
		List<FlowProjectModel> modelList= new ArrayList<FlowProjectModel>();
		for (Object object : list) {
			Object[] obj = (Object[])object;
			FlowProjectModel model = new FlowProjectModel();
			String id = obj[0]+"";
			long ids =NumberUtil.getLong(id);
			model.setId(ids);
			model.setName(String.valueOf(obj[1]));
			model.setAccount(NumberUtil.getDouble(obj[3]+""));
			model.setApr(NumberUtil.getDouble(obj[4]+""));
			model.setStatus(Byte.parseByte(obj[5]+""));
			model.setEachMoney(NumberUtil.getDouble(obj[6]+""));
			model.setStartCopies(NumberUtil.getInt(obj[7]+""));
			model.setTotalCopies(NumberUtil.getInt(obj[8]+""));
			model.setYesCopies(NumberUtil.getInt(obj[9]+""));
			model.setTimeLimit(NumberUtil.getInt(obj[10]+""));
			model.setRepayStyle(NumberUtil.getInt(obj[11]+""));
			model.setAddTime((Date)obj[13]);
			model.setUuid(obj[15]+"");
			model.setIsDay(Byte.parseByte(obj[18]+""));
			modelList.add(model);
		}
		
		return modelList;
	}

	@Override
	public PageDataList<FlowProjectModel> getIndexFlowList(
			FlowProjectModel model) {
		 QueryParam param = QueryParam.getInstance();
		 param.addPage(model.getPage(), model.getRows());
		// param.addParam("status", Operators.NOTEQ,"3");
		 param.addOrder(OrderType.DESC, "verifyTime");
		 flowOrder(param, model);
		 PageDataList<AdditionalFlowProject> pageFlowList = findPageList(param);
		 List<FlowProjectModel> flowModelList = new ArrayList<FlowProjectModel>();
		 PageDataList<FlowProjectModel> flowModelPageList = new PageDataList<FlowProjectModel>();
		 
		 for (AdditionalFlowProject flow : pageFlowList.getList()) {
			FlowProjectModel pmodel = FlowProjectModel.instance(flow);
			pmodel.setUser(null);
			flowModelList.add(pmodel);
		}
		 
		 flowModelPageList.setPage(pageFlowList.getPage());
		 flowModelPageList.setList(flowModelList);
		return flowModelPageList;
	}
    
	/**
	 * 我要投资流转标排序使用
	 * @param param
	 * @param model
	 */
	private void flowOrder(QueryParam param,FlowProjectModel model){
		//排序顺序
		switch (model.getOrder()) {
		case 1:  //金额升序
			param.addOrder(OrderType.DESC, "account");
			
			break;
			
		case -1: //金额倒序
			param.addOrder(OrderType.ASC, "account");
			break;
			
		case 2:// 利率升序
			param.addOrder(OrderType.DESC, "apr");
			break;
			
		case -2: //利率倒序
			param.addOrder(OrderType.ASC, "apr");
		
			break;

		default:
			param.addOrder(OrderType.DESC, "apr");
			break;
		}
		
		//利率搜索
		switch (model.getAprSearch()) {
		case 1:
			param.addParam("apr", Operators.LTE, 6);
			break;
		case 2:
			param.addParam("apr", Operators.GT, 6);
			param.addParam("apr", Operators.LTE, 12);
			break;
		case 3:
			param.addParam("apr", Operators.GT,12);
			param.addParam("apr", Operators.LTE, 18);
			break;
			
		case 4:
			param.addParam("apr", Operators.GT,18);
			param.addParam("apr", Operators.LTE, 20);
			break;
			
		case 5: 
			param.addParam("apr", Operators.GT,20);
			break;

		default:
			break;
		}
		
		//期限搜索
		switch (model.getTimeSearch()) {
		case 2:
			//比较特殊，按天计算
			param.addParam("isDay", Operators.EQ, 1);
			param.addParam("timeLimit", Operators.LTE, 30);
			break;
		case 3:
			param.addParam("isDay", Operators.EQ, 0);
			param.addParam("timeLimit", Operators.GTE, 1);
			param.addParam("timeLimit", Operators.LTE, 3);
			break;
		case 4:
			param.addParam("isDay", Operators.EQ, 0);
			param.addParam("timeLimit", Operators.GT, 3);
			param.addParam("timeLimit", Operators.LTE, 6);
			break;
			
		case 5:
			param.addParam("isDay", Operators.EQ, 0);
			param.addParam("timeLimit", Operators.GT, 6);
			param.addParam("timeLimit", Operators.LTE, 12);
			break;
		default:
			break;
		}
		
		//金额搜索
		switch (model.getMoneySearch()) {
		case 2:
			param.addParam("account", Operators.LTE, 10000);
			break;
		case 3:
			param.addParam("account", Operators.GT, 10000);
			param.addParam("account", Operators.LTE, 50000);
			break;
		case 4:
			param.addParam("account", Operators.GT, 50000);
			param.addParam("account", Operators.LTE,  100000);
			break;
		case 5:
			param.addParam("account", Operators.GT, 100000);
			param.addParam("account", Operators.LTE,  200000);
			break;
		case 6:
			param.addParam("account", Operators.GT, 200000);
			param.addParam("account", Operators.LTE,  500000);
			break;
		case 7:
			param.addParam("account", Operators.GT,  500000);
			break;
		default:
			break;
		}
		
		if (model.getQueryStatus()!=99) {
			param.addParam("status", Operators.EQ, model.getQueryStatus());
		}else{
			param.addParam("status", Operators.NOTEQ, 0);
			param.addParam("status", Operators.NOTEQ, 2);
		}
		
		if (StringUtil.isNotBlank(model.getSearchName())) {
			param.addParam("name", Operators.LIKE, model.getSearchName().trim());
			
		}
		
		
	}
	
    
}
